﻿using System;
using VIT.Web.UI;
using VIT.Web.Controls;
using VIT.Provider;
using System.Web.UI.WebControls;
using VIT.Library.Web;

namespace VIT.Web.Admin
{
    public partial class ModuleEdit : VIT.Web.UI.VITComponent
    {
        VIT.Provider.Module obj;
        protected void Page_Load(object sender, EventArgs e)
        {
            VIT.Provider.Language language = new VIT.Provider.Language();
            obj = new VIT.Provider.Module();
            try
            {
                obj.Id = Convert.ToInt32(Request["sId"]);
            }
            catch
            {
                obj.Id = -1;
            }
            if (obj.Id < 0) ButtonInsert.Visible = true; else ButtonModify.Visible = true;

            string currentLang = Request["sLang"];
            obj.LanguageId = (string.IsNullOrEmpty(currentLang)) ? Session["language"].ToString() : currentLang;
            obj.GetSingle();
            if (!IsPostBack)
            {
                loadLanguages(language, obj.LanguageId);
                //loadTemplate();
                loadModule();
                loadData();
            }
        }

        private void loadData()
        {
            if (obj.Id != 0)
            {
                loadTemplate();
                ddlTemplate.SelectedValue = obj.TemplateName;

                if (obj.ComponentName.Length > 0)
                {
                    rblName.Items[1].Selected = true;

                    loadComponent();
                    ddlComponent.SelectedValue = obj.ComponentName;

                    loadView(obj.ComponentName);
                    ddlView.SelectedValue = obj.ViewName;

                    loadComponentPosition(obj.ComponentName);
                    ddlPosition.SelectedValue = obj.ComponentPosition;
                }
                else
                {
                    rblName.Items[0].Selected = true;

                    loadTemplatePosition(obj.TemplateName);
                    ddlPosition.SelectedValue = obj.TemplatePosition;
                }
                ddlModule.Enabled = false;
                ddlModule.SelectedValue = obj.Name;
                txtTitle.Text = obj.Title;
                txtParam.Text = obj.Param;
                txtOrder.Text = obj.Orders.ToString();
                chkPublish.Checked = obj.Published;
            }
            else
            {
                loadParams(ddlModule.SelectedValue); // param mac dinh
                ddlModule.Enabled = true;
            }
        }

        private void loadLanguages(VIT.Provider.Language language, string selectedId)
        {
            ddlLang.DataSource = language.getAll();
            ddlLang.DataTextField = "Name";
            ddlLang.DataValueField = "Id";
            ddlLang.DataBind();

            if (string.IsNullOrEmpty(selectedId)) ddlLang.SelectedValue = Session["language"].ToString();
            else ddlLang.SelectedValue = selectedId;
        }

        private void loadTemplate()
        {
            ddlTemplate.Items.Clear();
            string TemplatePath = URLBaseDir + "Templates";
            ddlTemplate.Items.Add(" -- Chọn Template -- ");
            foreach(var item in MainCore.ShowDirectories(TemplatePath))
            {
                ListItem li = new ListItem(item, item);
                ddlTemplate.Items.Add(li);
            }
        }
        private void loadComponent()
        {
            ddlComponent.Items.Clear();
            string ModulePath = URLBaseDir + "Components";
            ddlComponent.Items.Add(" -- Chọn Component -- ");
            foreach (var item in MainCore.ShowDirectories(ModulePath))
            {
                ListItem li = new ListItem(item, item);
                ddlComponent.Items.Add(li);
            }
        }
        private void loadTemplatePosition(string templateName)
        {
            ddlPosition.Items.Clear();
            string xmlPositionPath = URLBaseDir + "Templates/" + templateName + "/Position.xml";
            ddlPosition.Items.Add(" -- Chọn vị trí -- ");
            foreach (var item in MainCore.getPositions(xmlPositionPath))
            {
                ListItem li = new ListItem(item.Value, item.Key);
                ddlPosition.Items.Add(li);
            }
        }
        private void loadView(string component)
        {
            ddlView.Items.Clear();
            string componentPath = URLBaseDir + "Components/" + component;
            
            ddlView.Items.Add(" -- Chọn vị trí -- ");
            foreach (var item in MainCore.getFileInFolder(componentPath,"aspx"))
            {
                ListItem li = new ListItem(item, item);
                ddlView.Items.Add(li);
            }
        }
        private void loadComponentPosition(string component)
        {
            ddlPosition.Items.Clear();
            string xmlPositionPath = URLBaseDir + "Components/" + component + "/Position.xml";
            ddlPosition.Items.Add(" -- Chọn vị trí -- ");
            foreach (var item in MainCore.getPositions(xmlPositionPath))
            {
                ListItem li = new ListItem(item.Value, item.Key);
                ddlPosition.Items.Add(li);
            }
        }
        private void loadModule()
        {
            string ModulePath = URLBaseDir + "Modules";
            ddlModule.Items.Add(" -- Chọn Module -- ");
            foreach (var item in MainCore.getModules(ModulePath))
            {
                ListItem li = new ListItem(item.Value, item.Key);
                ddlModule.Items.Add(li);
            }
        }
        private void loadParams(string moduleName)
        {
            try
            {
                string xmlParamPath = URLBaseDir + "Modules/" + moduleName + "/Readme.xml";
                txtParam.Text = MainCore.getParams(xmlParamPath);
            }
            catch
            {
                txtParam.Text = "";
            }
        }

        private void save()
        {
            if (ddlTemplate.SelectedIndex > 0)
            {
                obj.TemplateName = ddlTemplate.Text;

                if (ddlPosition.SelectedIndex > 0)
                {
                    if (rblName.Items[0].Selected == true)
                    {
                        //obj.TemplateName = ddlTemplate.Text;
                        obj.TemplatePosition = ddlPosition.SelectedValue;
                    }
                    else
                    {
                        //if(ddlTemplate.SelectedIndex > 0) obj.TemplateName = ddlTemplate.Text;
                        obj.ComponentPosition = ddlPosition.SelectedValue;
                        obj.ViewName = ddlView.Text;
                        obj.ComponentName = ddlComponent.Text;
                    }

                    obj.Name = ddlModule.SelectedValue;
                    obj.Param = txtParam.Text;
                    obj.LanguageId = ddlLang.SelectedValue;
                    obj.Title = txtTitle.Text;
                    obj.Orders = int.Parse(txtOrder.Text);
                    obj.Published = chkPublish.Checked;

                    if (obj.Save()) setTrueVisible(lblThanhCong);
                    else setTrueVisible(lblThatBai);
                }
                else setTrueVisible(lblViTri);
            }
            else setTrueVisible(lblTemplate);
            
        }

        private void setTrueVisible(System.Web.UI.HtmlControls.HtmlGenericControl control)
        {
            lblThanhCong.Visible = false;
            lblThatBai.Visible = false;
            lblViTri.Visible = false;

            control.Visible = true;
        }

        protected void Save_Click(object sender, EventArgs e)
        {
            save();
            RedirectComponent("Config", "ModuleList");
        }

        protected void Apply_Click(object sender, EventArgs e)
        {
            save();
            loadData();
        }

        protected void Cancel_Click(object sender, EventArgs e)
        {
            RedirectComponent("Config", "ModuleList");
        }
        
        protected void ddlTemplate_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (rblName.SelectedIndex == 0) loadTemplatePosition(ddlTemplate.SelectedItem.Text);
            else loadComponentPosition(ddlComponent.SelectedItem.Text);
        }
        protected void ddlComponent_SelectedIndexChanged(object sender, EventArgs e)
        {
            loadView(ddlComponent.SelectedItem.Text);
            loadComponentPosition(ddlComponent.SelectedItem.Text);
        }
        protected void ddlModule_SelectedIndexChanged(object sender, EventArgs e)
        {
            loadParams(ddlModule.SelectedItem.Value);
        }

        protected void Unnamed1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //ddlTemplate.Items.Clear();
            if (rblName.Items[0].Selected == true)
            {
                loadTemplate();
                trComp.Visible = false;
                trView.Visible = false;
            }
            else
            {
                loadTemplate();
                loadComponent();
                trComp.Visible = true;
                trView.Visible = true;
            }
        }
    }
}
